package com.ruoyi.serverinfo.dao;

import com.ruoyi.serverinfo.entity.TshockServerInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.UUID;

@Repository
public class TshockServerDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    /**
     * 保存服务器信息
     */
    public int save(TshockServerInfo serverInfo) {
        // 生成UUID
        if (serverInfo.getUuid() == null || serverInfo.getUuid().isEmpty()) {
            serverInfo.setUuid(UUID.randomUUID().toString());
        }
        
        String sql = "INSERT INTO tshock_server_info (uuid, openid, server_ip, server_port, server_rest, " +
                "server_rest_key, server_name, server_description, avatar_url, banner_url, banner_type) " +
                "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
        
        return jdbcTemplate.update(sql, 
                serverInfo.getUuid(),
                serverInfo.getOpenid(),
                serverInfo.getServerIp(),
                serverInfo.getServerPort(),
                serverInfo.getServerRest(),
                serverInfo.getServerRestKey(),
                serverInfo.getServerName(),
                serverInfo.getServerDescription(),
                serverInfo.getAvatarUrl(),
                serverInfo.getBannerUrl(),
                serverInfo.getBannerType()
        );
    }

    /**
     * 更新服务器信息
     */
    public int update(TshockServerInfo serverInfo) {
        String sql = "UPDATE tshock_server_info SET openid = ?, server_ip = ?, server_port = ?, server_rest = ?, " +
                "server_rest_key = ?, server_name = ?, server_description = ?, avatar_url = ?, " +
                "banner_url = ?, banner_type = ? WHERE uuid = ?";
        
        return jdbcTemplate.update(sql,
                serverInfo.getOpenid(),
                serverInfo.getServerIp(),
                serverInfo.getServerPort(),
                serverInfo.getServerRest(),
                serverInfo.getServerRestKey(),
                serverInfo.getServerName(),
                serverInfo.getServerDescription(),
                serverInfo.getAvatarUrl(),
                serverInfo.getBannerUrl(),
                serverInfo.getBannerType(),
                serverInfo.getUuid()
        );
    }

    /**
     * 根据UUID删除服务器信息
     */
    public int deleteByUuid(String uuid) {
        String sql = "DELETE FROM tshock_server_info WHERE uuid = ?";
        return jdbcTemplate.update(sql, uuid);
    }

    /**
     * 根据UUID查询服务器信息
     */
    public TshockServerInfo findByUuid(String uuid) {
        String sql = "SELECT * FROM tshock_server_info WHERE uuid = ?";
        List<TshockServerInfo> list = jdbcTemplate.query(sql, 
                new BeanPropertyRowMapper<>(TshockServerInfo.class), uuid);
        return list.isEmpty() ? null : list.get(0);
    }

    /**
     * 根据openid查询用户的所有服务器
     */
    public List<TshockServerInfo> findByOpenid(String openid) {
        String sql = "SELECT * FROM tshock_server_info WHERE openid = ? ORDER BY created_time DESC";
        return jdbcTemplate.query(sql, 
                new BeanPropertyRowMapper<>(TshockServerInfo.class), openid);
    }

    /**
     * 查询所有服务器信息
     */
    public List<TshockServerInfo> findAll() {
        String sql = "SELECT * FROM tshock_server_info ORDER BY created_time DESC";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(TshockServerInfo.class));
    }
}
    